/*
 * This program is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3 
 * of the License, or (at your option) any later version.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3, 
 * the Appropriate Legal Notices must display the following words on all interactive user interfaces: 
 * "Copyright (C) 2005-2011 SplendidCRM Software, Inc. All rights reserved."
 */

var sPOPUP_VIEW_SORT_FIELD       = 'NAME';
var sPOPUP_VIEW_SORT_DIRECTION   = 'asc';
var sPOPUP_VIEW_MODULE_NAME      = '';
var sPOPUP_VIEW_GRID_NAME        = '';
var sPOPUP_VIEW_SEARCH_FILTER    = '';
var bPOPUP_VIEW_MULTI_SELECT     = false;
var cbPopupViewUI_Main_Clicked   = null;

function PopupViewUI_Clear(sLayoutPanel, sMODULE_NAME)
{
	try
	{
		var divMainLayoutPanel = document.getElementById(sLayoutPanel);
		if ( divMainLayoutPanel != null && divMainLayoutPanel.childNodes != null )
		{
			while ( divMainLayoutPanel.childNodes.length > 0 )
			{
				divMainLayoutPanel.removeChild(divMainLayoutPanel.firstChild);
			}
		}
		if ( divMainLayoutPanel == null )
		{
			alert('PopupViewUI_Clear: ' + sLayoutPanel + ' does not exist');
			return;
		}
		SplendidUI_ListHeader(sLayoutPanel, sMODULE_NAME + '.LBL_LIST_FORM_TITLE');
		
		var ctlPopupViewButtons = document.createElement('div');
		ctlPopupViewButtons.id = sLayoutPanel + '_ctlPopupView';
		divMainLayoutPanel.appendChild(ctlPopupViewButtons);
		
		var btnClear       = document.createElement('input');
		btnClear.id        = sLayoutPanel + '_ctlPopupView_btnClear';
		btnClear.type      = 'button';
		btnClear.className = 'button';
		btnClear.title     = L10n.Term('.LBL_CLEAR_BUTTON_TITLE');
		btnClear.value     = L10n.Term('.LBL_CLEAR_BUTTON_LABEL');
		btnClear.style.paddingLeft  = 10;
		btnClear.style.paddingRight = 10;
		btnClear.style.marginRight  = 2;
		btnClear.style.marginBottom = 4;
		btnClear.onclick = BindArguments(cbPopupViewUI_Main_Clicked, 1, { 'ID': '', 'NAME': '' } );
		ctlPopupViewButtons.appendChild(btnClear);
		
		var btnCancel = document.createElement('input');
		btnCancel.id        = sLayoutPanel + '_ctlPopupView_btnCancel';
		btnCancel.type      = 'button';
		btnCancel.className = 'button';
		btnCancel.title     = L10n.Term('.LBL_CANCEL_BUTTON_TITLE');
		btnCancel.value     = L10n.Term('.LBL_CANCEL_BUTTON_LABEL');
		btnCancel.style.paddingLeft  = 10;
		btnCancel.style.paddingRight = 10;
		btnCancel.style.marginLeft   = 2;
		btnCancel.style.marginBottom = 4;
		btnCancel.onclick = BindArguments(cbPopupViewUI_Main_Clicked, -2, null );
		ctlPopupViewButtons.appendChild(btnCancel);
		
		// <table id="ctlListView_grdMain" class="listView" cellspacing="1" cellpadding="3" rules="all" border="0" border="1" width="100%">
		var ctlListView_grdMain = document.createElement('table');
		ctlListView_grdMain.id        = sLayoutPanel + '_ctlListView_grdMain';
		ctlListView_grdMain.width     = '100%';
		ctlListView_grdMain.className = 'listView';
		divMainLayoutPanel.appendChild(ctlListView_grdMain);
	}
	catch(e)
	{
		SplendidError.SystemMessage(SplendidError.FormatError(e, 'PopupViewUI_Clear'));
	}
}

function PopupViewUI_Reset(sLayoutPanel, sMODULE_NAME)
{
	try
	{
		sPOPUP_VIEW_SORT_FIELD     = 'NAME';
		sPOPUP_VIEW_SORT_DIRECTION = 'asc';
		sPOPUP_VIEW_SEARCH_FILTER  = '';
		PopupViewUI_Clear(sLayoutPanel, sMODULE_NAME);
	}
	catch(e)
	{
		SplendidError.SystemMessage(SplendidError.FormatError(e, 'PopupViewUI_Reset'));
	}
}

function PopupViewUI_Sort(sLayoutPanel, sActionsPanel, sFIELD_NAME, sDIRECTION)
{
	try
	{
		sPOPUP_VIEW_SORT_FIELD     = sFIELD_NAME;
		sPOPUP_VIEW_SORT_DIRECTION = sDIRECTION;
		SplendidError.SystemMessage('Sorting ' + sFIELD_NAME + ' ' + sDIRECTION);
		var bgPage = chrome.extension.getBackgroundPage();
		// 10/04/2011 Paul.  The session might have timed-out, so first check if we are authenticated. 
		bgPage.AuthenticatedMethod(function(status, message)
		{
			if ( status == 1 )
			{
				PopupViewUI_LoadModule(sLayoutPanel, sActionsPanel, sPOPUP_VIEW_MODULE_NAME, sPOPUP_VIEW_GRID_NAME, sPOPUP_VIEW_SEARCH_FILTER, function(status, message)
				{
					if ( status == 1 )
					{
						SplendidError.SystemMessage('');
					}
					else
					{
						SplendidError.SystemMessage(message);
					}
				});
			}
			else
			{
				SplendidError.SystemMessage(message);
			}
		});
	}
	catch(e)
	{
		SplendidError.SystemMessage(SplendidError.FormatError(e, 'PopupViewUI_Sort'));
	}
}

function PopupViewUI_Search(sLayoutPanel, sActionsPanel, sFilter)
{
	try
	{
		//alert('PopupViewUI_Search ' + dumpObj(arrData, 'arrData'));
		SplendidError.SystemMessage('Searching ');
		sPOPUP_VIEW_SEARCH_FILTER = sFilter;
		//alert('PopupViewUI_Search ' + sPOPUP_VIEW_SEARCH_FILTER);
		var bgPage = chrome.extension.getBackgroundPage();
		// 10/04/2011 Paul.  The session might have timed-out, so first check if we are authenticated. 
		bgPage.AuthenticatedMethod(function(status, message)
		{
			if ( status == 1 )
			{
				PopupViewUI_LoadModule(sLayoutPanel, sActionsPanel, sPOPUP_VIEW_MODULE_NAME, sPOPUP_VIEW_GRID_NAME, sPOPUP_VIEW_SEARCH_FILTER, function(status, message)
				{
					if ( status == 1 )
					{
						//SplendidError.SystemMessage(sPOPUP_VIEW_SEARCH_FILTER);
						SplendidError.SystemMessage('');
					}
					else
					{
						SplendidError.SystemMessage(message);
					}
				});
			}
			else
			{
				SplendidError.SystemMessage(message);
			}
		});
	}
	catch(e)
	{
		SplendidError.SystemMessage(SplendidError.FormatError(e, 'PopupViewUI_Search'));
	}
}

function PopupViewUI_CheckAll(chkMainCheckAll, sFieldID)
{
	try
	{
		var fld = document.getElementsByName(sFieldID);
		for (var i = 0; i < fld.length; i++)
		{
			if ( fld[i].type == 'checkbox' )
			{
				fld[i].checked = chkMainCheckAll.checked;
				if( fld[i].onclick != null )
				{
					fld[i].onclick();
				}
			}
		}
	}
	catch(e)
	{
		SplendidError.SystemAlert(e, 'PopupViewUI_CheckAll');
	}
}

function PopupViewUI_RenderHeader(sLayoutPanel, sActionsPanel, layout, tbody)
{
	// <tr class="listViewThS1">
	var tr = document.createElement('tr');
	tbody.appendChild(tr);
	tr.className = 'listViewThS1';
	if ( layout.length > 0 )
	{
		// 09/01/2011 Paul.  First column will be for actions. 
		var td = document.createElement('td');
		tr.appendChild(td);
		if ( bPOPUP_VIEW_MULTI_SELECT )
		{
			var chkMainCheckAll = document.createElement('input');
			chkMainCheckAll.id        = 'chkMainCheckAll';
			chkMainCheckAll.name      = 'chkMainCheckAll';
			chkMainCheckAll.type      = 'checkbox';
			chkMainCheckAll.className = 'checkbox';
			chkMainCheckAll.onclick   = BindArguments(PopupViewUI_CheckAll, chkMainCheckAll, 'chkMain');
			chkMainCheckAll.style.padding       = '2';
			chkMainCheckAll.style.verticalAlign = 'middle';
			td.appendChild(chkMainCheckAll);
		}
		for ( var nLayoutIndex in layout )
		{
			var lay = layout[nLayoutIndex];
			//alert(dumpObj(lay, 'lay'));
			var sCOLUMN_TYPE                = lay.COLUMN_TYPE               ;
			var sHEADER_TEXT                = lay.HEADER_TEXT               ;
			var sSORT_EXPRESSION            = lay.SORT_EXPRESSION           ;
			var sITEMSTYLE_WIDTH            = lay.ITEMSTYLE_WIDTH           ;
			var sITEMSTYLE_CSSCLASS         = lay.ITEMSTYLE_CSSCLASS        ;
			var sITEMSTYLE_HORIZONTAL_ALIGN = lay.ITEMSTYLE_HORIZONTAL_ALIGN;
			var sITEMSTYLE_VERTICAL_ALIGN   = lay.ITEMSTYLE_VERTICAL_ALIGN  ;
			var sITEMSTYLE_WRAP             = lay.ITEMSTYLE_WRAP            ;
			var sDATA_FIELD                 = lay.DATA_FIELD                ;
			var sDATA_FORMAT                = lay.DATA_FORMAT               ;
			var sURL_FIELD                  = lay.URL_FIELD                 ;
			var sURL_FORMAT                 = lay.URL_FORMAT                ;
			var sURL_TARGET                 = lay.URL_TARGET                ;
			var sLIST_NAME                  = lay.LIST_NAME                 ;
			var sURL_MODULE                 = lay.URL_MODULE                ;
			var sURL_ASSIGNED_FIELD         = lay.URL_ASSIGNED_FIELD        ;
			var sVIEW_NAME                  = lay.VIEW_NAME                 ;
			var sMODULE_NAME                = lay.MODULE_NAME               ;
			var sMODULE_TYPE                = lay.MODULE_TYPE               ;
			var sPARENT_FIELD               = lay.PARENT_FIELD              ;

			// <td style="white-space:nowrap;"><a href="javascript:PopupViewUI_Sort('NAME')">Name</a></td>
			td = document.createElement('td');
			tr.appendChild(td);
			if ( sSORT_EXPRESSION != null )
			{
				var a = document.createElement('a');
				td.appendChild(a);
				a.innerHTML = '<nobr>' + L10n.Term(sHEADER_TEXT) + '</nobr>';
				var img = document.createElement('img');
				td.appendChild(img);
				img.align       = 'absmiddle';
				img.height      = 10;
				img.width       = 8;
				img.borderWidth = 0;
				if ( sSORT_EXPRESSION == sPOPUP_VIEW_SORT_FIELD )
				{
					// img src="../App_Themes/Six/images/arrow_up.gif" align="absmiddle" style="border-width:0px;height:10px;width:8px;" />
					if ( sPOPUP_VIEW_SORT_DIRECTION == 'asc' )
					{
						img.src = sIMAGE_SERVER + 'App_Themes/Six/images/arrow_up.gif';
						a.href = 'javascript:PopupViewUI_Sort("' + sLayoutPanel + '", "' + sActionsPanel + '", "' + sSORT_EXPRESSION + '", "desc")';
					}
					else
					{
						img.src = sIMAGE_SERVER + 'App_Themes/Six/images/arrow_down.gif';
						a.href = 'javascript:PopupViewUI_Sort("' + sLayoutPanel + '", "' + sActionsPanel + '", "' + sSORT_EXPRESSION + '", "asc")';
					}
				}
				else
				{
					// img src="../App_Themes/Six/images/arrow.gif" align="absmiddle" style="border-width:0px;height:10px;width:8px;" />
					img.src = sIMAGE_SERVER + 'App_Themes/Six/images/arrow.gif';
					a.href = 'javascript:PopupViewUI_Sort("' + sLayoutPanel + '", "' + sActionsPanel + '", "' + sSORT_EXPRESSION + '", "asc")';
				}
			}
			else if ( sHEADER_TEXT != null )
			{
				var txt = document.createTextNode('<nobr>' + L10n.Term(sHEADER_TEXT) + '</nobr>');
				td.appendChild(txt);
			}
		}
	}
}

function PopupViewUI_RenderRow(sLayoutPanel, sActionsPanel, sLIST_MODULE_NAME, layout, tr, row)
{
	if ( layout.length > 0 )
	{
		// 09/01/2011 Paul.  First column will be for actions. 
		var td = document.createElement('td');
		tr.appendChild(td);
		td.style.whiteSpace = 'nowrap';

		if ( bPOPUP_VIEW_MULTI_SELECT )
		{
			var chkMain = document.createElement('input');
			chkMain.id        = 'chkMain_' + Sql.ToString(row['ID']).replace('-', '_');
			chkMain.name      = 'chkMain';
			chkMain.type      = 'checkbox';
			chkMain.className = 'checkbox';
			chkMain.Module    = sLIST_MODULE_NAME;
			chkMain.value     = row['ID'  ];
			chkMain.tooltip   = row['NAME'];
			//chkMain.onclick   = BindArguments(cbPopupViewUI_Main_Clicked, chkMain, sLIST_MODULE_NAME, row['ID'], row['NAME']);
			chkMain.style.padding       = '2';
			chkMain.style.verticalAlign = 'middle';
			// 09/25/2011 Paul.  IE does not allow you to set the type after it is added to the document. 
			td.appendChild(chkMain);
			// 10/04/2011 Paul.  IE8 requires that we set checked after appending. 
			if ( SelectionUI_IsSelected(row['ID']) )
				chkMain.checked = true;
		}

		for ( var nLayoutIndex in layout )
		{
			var lay = layout[nLayoutIndex];
			//alert(dumpObj(lay, 'lay'));
			var sCOLUMN_TYPE                = lay.COLUMN_TYPE               ;
			var sHEADER_TEXT                = lay.HEADER_TEXT               ;
			var sSORT_EXPRESSION            = lay.SORT_EXPRESSION           ;
			var sITEMSTYLE_WIDTH            = lay.ITEMSTYLE_WIDTH           ;
			var sITEMSTYLE_CSSCLASS         = lay.ITEMSTYLE_CSSCLASS        ;
			var sITEMSTYLE_HORIZONTAL_ALIGN = lay.ITEMSTYLE_HORIZONTAL_ALIGN;
			var sITEMSTYLE_VERTICAL_ALIGN   = lay.ITEMSTYLE_VERTICAL_ALIGN  ;
			var sITEMSTYLE_WRAP             = lay.ITEMSTYLE_WRAP            ;
			var sDATA_FIELD                 = lay.DATA_FIELD                ;
			var sDATA_FORMAT                = lay.DATA_FORMAT               ;
			var sURL_FIELD                  = lay.URL_FIELD                 ;
			var sURL_FORMAT                 = lay.URL_FORMAT                ;
			var sURL_TARGET                 = lay.URL_TARGET                ;
			var sLIST_NAME                  = lay.LIST_NAME                 ;
			var sURL_MODULE                 = lay.URL_MODULE                ;
			var sURL_ASSIGNED_FIELD         = lay.URL_ASSIGNED_FIELD        ;
			var sVIEW_NAME                  = lay.VIEW_NAME                 ;
			var sMODULE_NAME                = lay.MODULE_NAME               ;
			var sMODULE_TYPE                = lay.MODULE_TYPE               ;
			var sPARENT_FIELD               = lay.PARENT_FIELD              ;
			
			td = document.createElement('td');
			tr.appendChild(td);
			if ( sITEMSTYLE_WIDTH            != null ) td.width     = sITEMSTYLE_WIDTH           ;
			if ( sITEMSTYLE_CSSCLASS         != null ) td.className = sITEMSTYLE_CSSCLASS        ;
			if ( sITEMSTYLE_HORIZONTAL_ALIGN != null ) td.align     = sITEMSTYLE_HORIZONTAL_ALIGN;
			if ( sITEMSTYLE_VERTICAL_ALIGN   != null ) td.vAlign    = sITEMSTYLE_VERTICAL_ALIGN  ;
			
			if (   sCOLUMN_TYPE == 'BoundColumn' 
			    && (   sDATA_FORMAT == 'Date'
			        || sDATA_FORMAT == 'DateTime'
			        || sDATA_FORMAT == 'Currency'
			        || sDATA_FORMAT == 'Image'
			        || sDATA_FORMAT == 'MultiLine'
			       )
			   )
			{
				sCOLUMN_TYPE = 'TemplateColumn';
			}
			if ( sCOLUMN_TYPE == 'TemplateColumn' )
			{
				//alert(sDATA_FORMAT + ' ' + row[sDATA_FIELD]);
				if ( row[sDATA_FIELD] != null )
				{
					if ( sDATA_FORMAT == 'HyperLink' )
					{
						var a = document.createElement('a');
						td.appendChild(a);
						a.href      = 'javascript:void(0);';
						a.innerHTML = row[sDATA_FIELD];
						if ( cbPopupViewUI_Main_Clicked != null )
						{
							a.onclick = BindArguments(cbPopupViewUI_Main_Clicked, 1, { 'ID': row['ID'], 'NAME': row[sDATA_FIELD] } );
						}
					}
					else if ( sDATA_FORMAT == 'Date' )
					{
						var sDATA_VALUE = row[sDATA_FIELD];
						sDATA_VALUE = FromJsonDate(sDATA_VALUE, Security.USER_DATE_FORMAT());
						td.innerHTML = sDATA_VALUE;
					}
					else if ( sDATA_FORMAT == 'DateTime' )
					{
						var sDATA_VALUE = row[sDATA_FIELD];
						sDATA_VALUE = FromJsonDate(sDATA_VALUE, Security.USER_DATE_FORMAT() + ' ' + Security.USER_TIME_FORMAT());
						td.innerHTML = sDATA_VALUE;
					}
					else if ( sDATA_FORMAT == 'Currency' )
					{
						var sDATA_VALUE = row[sDATA_FIELD];
						td.innerHTML = sDATA_VALUE;
					}
					else if ( sDATA_FORMAT == 'MultiLine' )
					{
						var sDATA_VALUE = row[sDATA_FIELD];
						td.innerHTML = sDATA_VALUE;
					}
					else if ( sDATA_FORMAT == 'Image' )
					{
					}
					else if ( sDATA_FORMAT == 'JavaScript' )
					{
					}
					else if ( sDATA_FORMAT == 'Hover' )
					{
					}
				}
			}
			else if ( sCOLUMN_TYPE == 'BoundColumn' )
			{
				if ( row[sDATA_FIELD] != null )
				{
					if ( sLIST_NAME != null )
					{
						var sDATA_VALUE = L10n.Term('.' + sLIST_NAME + '.' + row[sDATA_FIELD]);
						// 10/04/2011 Paul.  Testing for NULL does not work, so just check if the list name matches the label. 
						if ( sDATA_VALUE == '.' + sLIST_NAME + '.' )
							sDATA_VALUE = '';
						td.innerHTML = sDATA_VALUE;
					}
					else
					{
						var sDATA_VALUE = row[sDATA_FIELD];
						td.innerHTML = sDATA_VALUE;
					}
				}
			}
		}
	}
}

function PopupViewUI_GridColumns(layout)
{
	var arrSelectFields = new Array();
	if ( layout.length > 0 )
	{
		for ( var nLayoutIndex in layout )
		{
			var lay = layout[nLayoutIndex];
			var sSORT_EXPRESSION            = lay.SORT_EXPRESSION           ;
			var sDATA_FIELD                 = lay.DATA_FIELD                ;
			var sDATA_FORMAT                = lay.DATA_FORMAT               ;
			var sURL_FIELD                  = lay.URL_FIELD                 ;
			var sURL_ASSIGNED_FIELD         = lay.URL_ASSIGNED_FIELD        ;
			var sPARENT_FIELD               = lay.PARENT_FIELD              ;
			
			if ( sDATA_FORMAT == 'Hover' )
				continue;
			if ( sDATA_FIELD != null && sDATA_FIELD.length > 0 )
			{
				arrSelectFields.push(sDATA_FIELD);
			}
			if ( sSORT_EXPRESSION != null && sSORT_EXPRESSION.length > 0 )
			{
				if ( sDATA_FIELD != sSORT_EXPRESSION )
					arrSelectFields.push(sSORT_EXPRESSION);
			}
			if ( sURL_FIELD != null && sURL_FIELD.length > 0 )
			{
				if ( sURL_FIELD.indexOf(' ') >= 0 )
				{
					var arrURL_FIELD = sURL_FIELD.split(' ');
					for ( var i in arrURL_FIELD )
					{
						var s = arrURL_FIELD[i];
						if ( s.indexOf('.') == -1 && s.length > 0 )
						{
							arrSelectFields.push(s);
						}
					}
				}
				else if ( sURL_FIELD.indexOf('.') == -1 )
				{
					arrSelectFields.push(sURL_FIELD);
				}
				if ( sURL_ASSIGNED_FIELD != null && sURL_ASSIGNED_FIELD.length > 0 )
				{
					arrSelectFields.push(sURL_ASSIGNED_FIELD);
				}
			}
			if ( sPARENT_FIELD != null && sPARENT_FIELD.length > 0 )
			{
				arrSelectFields.push(sPARENT_FIELD);
			}
		}
	}
	return arrSelectFields.join(',');
}

function PopupViewUI_LoadModule(sLayoutPanel, sActionsPanel, sMODULE_NAME, sGRID_NAME, sPOPUP_VIEW_SEARCH_FILTER, callback)
{
	try
	{
		var bgPage = chrome.extension.getBackgroundPage();
		//var layout = bgPage.SplendidCache.GridViewColumns(sGRID_NAME);
		bgPage.ListView_LoadLayout(sGRID_NAME, function(status, message)
		{
			if ( status == 1 )
			{
				// 10/03/2011 Paul. ListView_LoadLayout returns the layout. 
				var layout = message;
				var sSELECT_FIELDS = PopupViewUI_GridColumns(layout);
				bgPage.ListView_LoadModule(sMODULE_NAME, sPOPUP_VIEW_SORT_FIELD, sPOPUP_VIEW_SORT_DIRECTION, sSELECT_FIELDS, sPOPUP_VIEW_SEARCH_FILTER, function(status, message)
				{
					if ( status == 1 )
					{
						// 10/04/2011 Paul.  ListView_LoadModule returns the row. 
						var rows = message;
						PopupViewUI_Clear(sLayoutPanel, sMODULE_NAME);
						var ctlListView_grdMain = document.getElementById(sLayoutPanel + '_ctlListView_grdMain');
						var tbody = document.createElement('tbody');
						ctlListView_grdMain.appendChild(tbody);
						
						PopupViewUI_RenderHeader(sLayoutPanel, sActionsPanel, layout, tbody);
						
						for ( var i = 0; i < rows.length; i++ )
						{
							var tr = document.createElement('tr');
							tbody.appendChild(tr);
							if ( i % 2 == 0 )
								tr.className = 'oddListRowS1';
							else
								tr.className = 'evenListRowS1';
							
							var row = rows[i];
							PopupViewUI_RenderRow(sLayoutPanel, sActionsPanel, sMODULE_NAME, layout, tr, row);
						}
						callback(1, null);
					}
					else
					{
						callback(status, message);
					}
				});
			}
			else
			{
				callback(status, message);
			}
		});
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'PopupViewUI_LoadModule'));
	}
}

function PopupViewUI_Load(sLayoutPanel, sActionsPanel, sMODULE_NAME, bMultiSelect, callback)
{
	try
	{
		var sGRID_NAME = sMODULE_NAME + '.PopupView' + sPLATFORM_LAYOUT;
		sPOPUP_VIEW_MODULE_NAME    = sMODULE_NAME;
		sPOPUP_VIEW_GRID_NAME      = sGRID_NAME  ;
		sPOPUP_VIEW_SEARCH_FILTER  = ''          ;
		bPOPUP_VIEW_MULTI_SELECT   = bMultiSelect;
		cbPopupViewUI_Main_Clicked = callback    ;

		var bgPage = chrome.extension.getBackgroundPage();
		// 10/04/2011 Paul.  The session might have timed-out, so first check if we are authenticated. 
		bgPage.AuthenticatedMethod(function(status, message)
		{
			if ( status == 1 )
			{
				bgPage.Terminology_LoadModule(sMODULE_NAME, function(status, message)
				{
					if ( status == 0 || status == 1 )
					{
						// 09/10/2011 Paul.  Make sure to load the layout first as it might be needed inside SearchViewUI_SearchForm, or PopupViewUI_LoadModule, which run in parallel. 
						bgPage.ListView_LoadLayout(sGRID_NAME, function(status, message)
						{
							if ( status == 0 || status == 1 )
							{
								// 10/03/2011 Paul. ListView_LoadLayout returns the layout. 
								var layout = message;
								var rowDefaultSearch = null;
								SearchViewUI_Load(sLayoutPanel, sActionsPanel, sMODULE_NAME, sMODULE_NAME + '.SearchPopup' + sPLATFORM_LAYOUT, rowDefaultSearch, false, PopupViewUI_Search, function(status, message)
								{
									if ( status == -1 )
										callback(status, message);
								});
								PopupViewUI_LoadModule(sLayoutPanel, sActionsPanel, sMODULE_NAME, sGRID_NAME, sPOPUP_VIEW_SEARCH_FILTER, function(status, message)
								{
									if ( status == -1 )
										callback(status, message);
								});
							}
							else
							{
								callback(status, message);
							}
						});
					}
					else
					{
						callback(status, message);
					}
				});
			}
			else
			{
				callback(-1, message);
			}
		});
	}
	catch(e)
	{
		callback(-1, SplendidError.FormatError(e, 'PopupViewUI_Load'));
	}
}


